﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using cash_desk_model.src.security;

namespace cash_desk
{
    public partial class LoginForm : Form
    {
        public LoginForm()
        {            
            InitializeComponent();
        }

        private void loginButton_Click(object sender, EventArgs e)
        {
            errorLabel.Visible = false;
            if (this.userNameTextBox.Text.Length > 0 && this.passwordTextBox.Text.Length > 0)
            {
                SecurityService service = new SecurityService();
                SecurityService.SetupDBConn();

                if (service.CheckUser(this.userNameTextBox.Text))
                {
                    if (service.AthenticateUser(this.userNameTextBox.Text, this.passwordTextBox.Text))
                    {
                        //MessageBox.Show("Hello");
                        this.Hide();
                        this.errorLabel.Text = "";
                        //CasherForm.CurrentUser = userNameTextBox.Text;
                        cash_desk_model.src.security.User audentificatedUser = service.GetAuthenticatedUser(this.userNameTextBox.Text);
                        CashierForm casherForm = new CashierForm(this, audentificatedUser);
                        this.userNameTextBox.Text = "";
                        this.passwordTextBox.Text = "";
                        casherForm.ShowDialog();
                        this.Show();
                        userNameTextBox.Focus();
                    }
                    else
                    {
                        //MessageBox.Show("Invalid Password");
                        errorLabel.Visible = true;
                        this.errorLabel.Text = "პაროლი არ არის სწორი";
                        passwordTextBox.Text = "";
                        passwordTextBox.Focus();
                    }
                }
                else
                {
                    //MessageBox.Show("Invalid User");
                    errorLabel.Visible = true;
                    this.errorLabel.Text = "დაურეგისტრირებელი მომხმარებელი";
                    userNameTextBox.Text = "";
                    passwordTextBox.Text = "";
                    userNameTextBox.Focus();
                }

                SecurityService.DestroyDBConn();
            }
        }
    }
}
